package service

import (
	"context"
	pb "digitalhuman/api/storyboard/v1"
	"digitalhuman/internal/biz"
)

type StoryboardService struct {
	pb.UnimplementedStoryboardServer
	storyBiz *biz.StoryboardBiz
}

func NewStoryboardService(sb *biz.StoryboardBiz) *StoryboardService {
	return &StoryboardService{
		storyBiz: sb,
	}
}

func (s *StoryboardService) UpdateStoryboard(ctx context.Context, req *pb.UpdateStoryboardRequest) (*pb.UpdateStoryboardReply, error) {
	return &pb.UpdateStoryboardReply{}, nil
}
func (s *StoryboardService) DeleteStoryboard(ctx context.Context, req *pb.DeleteStoryboardRequest) (*pb.DeleteStoryboardReply, error) {
	uid := ctx.Value("user_id").(float64)
	err := s.storyBiz.DeleteStory(int(req.Id), int(uid))
	if err != nil {
		return nil, err
	}

	return &pb.DeleteStoryboardReply{Code: 0, Message: "删除成功"}, nil
}
func (s *StoryboardService) GetStoryboard(ctx context.Context, req *pb.GetStoryboardRequest) (*pb.GetStoryboardReply, error) {
	ret, err := s.storyBiz.GetStory(ctx, int(req.Id))
	if err != nil {
		return nil, err
	}
	return ret, nil
}
func (s *StoryboardService) ListStoryboard(ctx context.Context, req *pb.ListStoryboardRequest) (*pb.ListStoryboardReply, error) {
	uid := ctx.Value("user_id").(float64)
	ret, err := s.storyBiz.ListStory(ctx, int(uid), 0)
	if err != nil {
		return nil, err
	}

	return ret, nil
}

func (s *StoryboardService) DraftStoryboard(ctx context.Context, req *pb.ListStoryboardRequest) (*pb.ListStoryboardDraftReply, error) {
	uid := ctx.Value("user_id").(float64)
	ret, err := s.storyBiz.ListStoryDraft(ctx, int(uid))
	if err != nil {
		return nil, err
	}

	return ret, nil
}

func (s *StoryboardService) SubmitStoryboard(ctx context.Context, req *pb.ListStoryboardRequest) (*pb.ListStoryboardSubmitReply, error) {
	uid := ctx.Value("user_id").(float64)
	ret, err := s.storyBiz.ListStorySubmit(ctx, int(uid))
	if err != nil {
		return nil, err
	}

	return ret, nil
}

func (s *StoryboardService) CreateStoryboard(ctx context.Context, req *pb.CreateStoryboardRequestL) (*pb.CreateStoryboardReply, error) {
	// 存草稿 创建 都走这里
	userId := ctx.Value("user_id").(float64)
	storyboardId, err := s.storyBiz.CreateStory(ctx, req, int(userId))
	if err != nil {
		return nil, err
	}

	return &pb.CreateStoryboardReply{Code: 0, Message: "操作成功", StoryboardId: int32(storyboardId)}, nil
}

func (s *StoryboardService) CallBackStoryboard(ctx context.Context, req *pb.CallBackProduceRequest) (*pb.CallBackProduceReply, error) {
	s.storyBiz.CallBackProduce(ctx, req)

	return &pb.CallBackProduceReply{}, nil
}
